/*     */ package com.cr.web.controller.common;
/*     */ 
/*     */ import com.cr.common.config.RuoYiConfig;
/*     */ import com.cr.common.config.ServerConfig;
/*     */ import com.cr.common.core.domain.AjaxResult;
/*     */ import com.cr.common.utils.MyStringUtils;
import org.apache.commons.lang3.StringUtils;
/*     */ import com.cr.common.utils.file.FileUploadUtils;
/*     */ import com.cr.common.utils.file.FileUtils;
/*     */ import java.io.OutputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.stereotype.Controller;
/*     */ import org.springframework.web.bind.annotation.GetMapping;
/*     */ import org.springframework.web.bind.annotation.PostMapping;
/*     */ import org.springframework.web.bind.annotation.ResponseBody;
/*     */ import org.springframework.web.multipart.MultipartFile;
/*     */ 
/*     */ @Controller
/*     */ public class CommonController {
/*  32 */   private static final Logger log = LoggerFactory.getLogger(CommonController.class);
/*     */   
/*     */   @Autowired
/*     */   private ServerConfig serverConfig;
/*     */   
/*     */   private static final String FILE_DELIMETER = ",";
/*     */   
/*     */   @GetMapping({"common/download"})
/*     */   public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
/*     */     try {
/*  50 */       if (!FileUtils.checkAllowDownload(fileName))
/*  52 */         throw new Exception(MyStringUtils.format("文件名称({})非法，不允许下载。 ", new Object[] { fileName }));
/*  54 */       String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
/*  55 */       String filePath = RuoYiConfig.getDownloadPath() + fileName;
/*  57 */       response.setContentType("application/octet-stream");
/*  58 */       FileUtils.setAttachmentResponseHeader(response, realFileName);
/*  59 */       FileUtils.writeBytes(filePath, (OutputStream)response.getOutputStream());
/*  60 */       if (delete.booleanValue())
/*  62 */         FileUtils.deleteFile(filePath); 
/*  65 */     } catch (Exception e) {
/*  67 */       log.error("下载文件失败", e);
/*     */     } 
/*     */   }
/*     */   
/*     */   @PostMapping({"/common/upload"})
/*     */   @ResponseBody
/*     */   public AjaxResult uploadFile(MultipartFile file) throws Exception {
/*     */     try {
/*  81 */       String filePath = RuoYiConfig.getUploadPath();
/*  83 */       String fileName = FileUploadUtils.upload(filePath, file);
/*  84 */       String url = this.serverConfig.getUrl() + fileName;
/*  85 */       AjaxResult ajax = AjaxResult.success();
/*  86 */       ajax.put("fileName", fileName);
/*  87 */       ajax.put("url", url);
/*  88 */       ajax.put("name", file.getOriginalFilename());
/*  89 */       return ajax;
/*  91 */     } catch (Exception e) {
/*  93 */       return AjaxResult.error(e.getMessage());
/*     */     } 
/*     */   }
/*     */   
/*     */   @PostMapping({"/common/uploads"})
/*     */   @ResponseBody
/*     */   public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception {
/*     */     try {
/* 107 */       String filePath = RuoYiConfig.getUploadPath();
/* 108 */       List<String> fileNames = new ArrayList<>();
/* 109 */       List<String> urls = new ArrayList<>();
/* 110 */       for (MultipartFile file : files) {
/* 113 */         String fileName = FileUploadUtils.upload(filePath, file);
/* 114 */         String url = this.serverConfig.getUrl() + fileName;
/* 115 */         fileNames.add(fileName);
/* 116 */         urls.add(url);
/*     */       } 
/* 118 */       AjaxResult ajax = AjaxResult.success();
/* 119 */       ajax.put("fileNames", StringUtils.join(fileNames, ","));
/* 120 */       ajax.put("urls", StringUtils.join(urls, ","));
/* 121 */       return ajax;
/* 123 */     } catch (Exception e) {
/* 125 */       return AjaxResult.error(e.getMessage());
/*     */     } 
/*     */   }
/*     */   
/*     */   @GetMapping({"/common/download/resource"})
/*     */   public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response) throws Exception {
/*     */     try {
/* 138 */       if (!FileUtils.checkAllowDownload(resource))
/* 140 */         throw new Exception(MyStringUtils.format("资源文件({})非法，不允许下载。 ", new Object[] { resource }));
/* 143 */       String localPath = RuoYiConfig.getProfile();
/* 145 */       String downloadPath = localPath + StringUtils.substringAfter(resource, "/profile");
/* 147 */       String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
/* 148 */       response.setContentType("application/octet-stream");
/* 149 */       FileUtils.setAttachmentResponseHeader(response, downloadName);
/* 150 */       FileUtils.writeBytes(downloadPath, (OutputStream)response.getOutputStream());
/* 152 */     } catch (Exception e) {
/* 154 */       log.error("下载文件失败", e);
/*     */     } 
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\classes\com.zip!\com\cr\web\controller\common\CommonController.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */